package com.qire.manhua.view.refresh;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.opensource.svgaplayer.SVGAImageView;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.utils.EquipmentInfoUtil;
import com.qire.common.utils.SVGAPlayUtil;
import com.qire.manhua.R;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshKernel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.constant.SpinnerStyle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 首页跳跳板下拉刷新
 */
public class SeesawAnimationDropHeader extends FrameLayout implements RefreshHeader {
    //<editor-fold desc="成员变量">
    private RefreshState mState;
    protected RefreshKernel mRefreshKernel;
    protected RefreshLayout mRefreshLayout;

    private String text = "下拉加载...";
    private int ropDownDistance = 0;

    private ImageView imageView;
    private SVGAImageView svgaImageView;
    private SVGAPlayUtil playUtil;
    private TextView tipsTextView;

    private OnMovingListener movingListener;
    //</editor-fold>

    public SeesawAnimationDropHeader(Context context) {
        this(context, null);
    }

    public SeesawAnimationDropHeader(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SeesawAnimationDropHeader(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    //<editor-fold desc="View的自身行为">

    private void init() {
        LayoutParams imageViewLP = new LayoutParams(EquipmentInfoUtil.getScreenWidth(), EquipmentInfoUtil.getScreenHeight());
        imageViewLP.gravity = Gravity.BOTTOM;
        imageView = new ImageView(getContext());
        imageView.setLayoutParams(imageViewLP);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setImageResource(R.drawable.app_home_recommend_header_pic_seesaw_bg);

        LayoutParams seesawAnimationViewLP = new LayoutParams(LayoutParams.MATCH_PARENT, EquipmentInfoUtil.dip2px(30));
        seesawAnimationViewLP.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        seesawAnimationViewLP.bottomMargin = EquipmentInfoUtil.dip2px(28);
        svgaImageView = new SVGAImageView(getContext());
        svgaImageView.setLayoutParams(seesawAnimationViewLP);
        playUtil = new SVGAPlayUtil(svgaImageView);
        playUtil.addSVGAPath("svga/seesaw.svga");

        LayoutParams tipsTextViewLP = new LayoutParams(LayoutParams.MATCH_PARENT, EquipmentInfoUtil.dip2px(20));
        tipsTextView = new TextView(getContext());
        tipsTextViewLP.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        tipsTextViewLP.bottomMargin = EquipmentInfoUtil.dip2px(5);
        tipsTextView.setLayoutParams(tipsTextViewLP);
        tipsTextView.setGravity(Gravity.CENTER);
        tipsTextView.setText(text);
        tipsTextView.setTextSize(11);
        tipsTextView.setTextColor(Color.rgb(0x8c,0x8c,0x8c));

        addView(imageView);
        addView(svgaImageView);
        addView(tipsTextView);
        setClipChildren(false);
    }

    public void setOnMovingListener(OnMovingListener movingListener) {
        this.movingListener = movingListener;
    }

    //</editor-fold>

    //<editor-fold desc="RefreshHeader 可刷新头部接口">
    @NonNull
    @Override
    public View getView() {
        return this;
    }

    @NonNull
    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;
    }

    @Override
    public void setPrimaryColors(int... colors) {

    }

    @Override
    public void onInitialized(@NonNull RefreshKernel kernel, int height, int extendHeight) {
        mRefreshKernel = kernel;
        mRefreshLayout = mRefreshKernel.getRefreshLayout();
        // 刷新头的高度，这个决定了刷新下拉可以拉伸的距离基数，以及停留动画显示的高度。
        mRefreshLayout.setHeaderHeight(EquipmentInfoUtil.dip2px(30));
        mRefreshLayout.setHeaderTriggerRate(0.5f);
//        System.out.println("OwlAnimationDropHeader===== onInitialized");
    }

    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
        if(isDragging) {
            onPulling(percent, offset, height, maxDragHeight);
        } else {
            onReleasing(percent, offset, height, maxDragHeight);
        }

        if(ObjectUtil.nonNull(movingListener)) {
            movingListener.onMoving(isDragging, offset);
        }
    }

    private void onPulling(float percent, int offset, int height, int extendHeight) {
        ropDownDistance = offset;
    }

    private void onReleasing(float percent, int offset, int height, int extendHeight) {
        ropDownDistance = offset;
    }

    @Override
    public void onReleased(RefreshLayout refreshLayout, int height, int extendHeight) {
//        System.out.println("OwlAnimationDropHeader===== onReleased");
    }

    @Override
    public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int extendHeight) {
//        System.out.println("OwlAnimationDropHeader===== onStartAnimator");
    }

    @Override
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
//        System.out.println("OwlAnimationDropHeader===== onFinish");
        if(success) {
            text = "加载完成";
        } else {
            text = "加载失败";
        }
        return 500;
    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {}

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
        mState = newState;

        if(oldState == RefreshState.None && newState == RefreshState.PullDownToRefresh) {
            text = "下拉加载...";
            playUtil.startAnimator();
            if(mRefreshLayout instanceof SmartRefreshLayout) {
                ((SmartRefreshLayout) mRefreshLayout).setClipChildren(false);
            }
        }

        if(newState == RefreshState.None) {
            playUtil.pauseSVGA();
            if(mRefreshLayout instanceof SmartRefreshLayout) {
                ((SmartRefreshLayout) mRefreshLayout).setClipChildren(true);
            }
        }
    }

    //</editor-fold>

    public interface OnMovingListener {
        void onMoving(boolean isDragging, int offset);
    }

}
